﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExtAspNet;

namespace HY.WEB.PQ
{
    public partial class ht_pq_edit : PageBase
    {
        public string whe = "", path = "", tab = "pq_ht";
        public string us, um, ubh;          
        protected void Page_Load(object sender, EventArgs e)
        {
            us = Session["ID"].ToString();
            um = Session["user"].ToString();
            ubh = Session["bh"].ToString();
            if (!IsPostBack)
            {
                if (!String.IsNullOrEmpty(Request["sid"]))
                {
                     hidsid.Text = Request["sid"].Trim();                
                }
                fill();
                fill2();
            }

        
        }

        //填 充ddl
        void fill()
        {

            //公司
            DataTable e = BLL.Bllbase.get_table("[company]");
            ddlcompany.Items.Clear();
            ddlcomp.Items.Clear();
            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddlcompany.Items.Add(new ExtAspNet.ListItem(e.Rows[i]["cname"].ToString().Trim(), e.Rows[i]["cbh"].ToString().Trim()));
                ddlcomp.Items.Add(new ExtAspNet.ListItem(e.Rows[i]["cname"].ToString().Trim(), e.Rows[i]["cbh"].ToString().Trim()));
          
            }
            ddlcompany.DataBind();
            ddlcomp.DataBind();

            ddlcompany.Items.Add(new ExtAspNet.ListItem("", ""));
            ddlcomp.Items.Add(new ExtAspNet.ListItem("", ""));


            //员工
            e = BLL.Bllbase.get_table("[pq_member]");
            ddlperson.Items.Clear();
            ddlsales.Items.Clear();

            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddlperson.Items.Add(new ExtAspNet.ListItem(e.Rows[i]["user_name"].ToString().Trim(), e.Rows[i]["sbh"].ToString().Trim()));
                ddlsales.Items.Add(new ExtAspNet.ListItem(e.Rows[i]["user_name"].ToString().Trim(), e.Rows[i]["sbh"].ToString().Trim()));
         
            }
            ddlperson.DataBind();
            ddlsales.DataBind();


            ddlsales.Items.Add(new ExtAspNet.ListItem("", ""));
            ddlperson.Items.Add(new ExtAspNet.ListItem("", ""));




            //校园
            e = BLL.Bllbase.get_table("[school]");
            ddlschool.Items.Clear();
            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddlschool.Items.Add(new ExtAspNet.ListItem(e.Rows[i]["schname"].ToString().Trim(), e.Rows[i]["schbh"].ToString().Trim()));
            }
            ddlschool.DataBind();
            ddlschool.Items.Add(new ExtAspNet.ListItem("", ""));

         
            //中介
            e = BLL.Bllbase.get_table("[zj]");
            ddlzj.Items.Clear();
            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddlzj.Items.Add(new ExtAspNet.ListItem(e.Rows[i]["zname"].ToString().Trim(), e.Rows[i]["zbh"].ToString().Trim()));
            }
            ddlzj.DataBind();
            ddlzj.Items.Add(new ExtAspNet.ListItem("", ""));


        }

       

        void fill2()
        {
            string dts; DateTime lastday;

            BLL.PQ.pq_ht S = new BLL.PQ.pq_ht();
            DataTable d=  S.GetList("sid="+hidsid.Text).Tables[0];


            hidsbh.Text = d.Rows[0]["htbh"].ToString().Trim();
            txtht.Text = d.Rows[0]["htbh"].ToString().Trim();

            txtzsales.Text = d.Rows[0]["zsales"].ToString().Trim();

            ddlperson.SelectedIndex = ddlperson.Items.IndexOf(ddlperson.Items.FindByText(d.Rows[0]["pbh"].ToString()));
            ddlcompany.SelectedIndex = ddlcompany.Items.IndexOf(ddlcompany.Items.FindByText(d.Rows[0]["pvendor"].ToString()));

           string ft=d.Rows[0]["ftype"].ToString().Trim();

            if(ft=="企业")
                ddlcomp.SelectedIndex = ddlcomp.Items.IndexOf(ddlcomp.Items.FindByValue(d.Rows[0]["fbh"].ToString()));
         
            if (ft == "业务员")
                ddlsales.SelectedIndex = ddlsales.Items.IndexOf(ddlsales.Items.FindByValue(d.Rows[0]["fbh"].ToString()));

            if (ft == "学校")
                ddlschool.SelectedIndex = ddlschool.Items.IndexOf(ddlschool.Items.FindByValue(d.Rows[0]["fbh"].ToString()));
        
            if (ft == "中介")
                ddlzj.SelectedIndex = ddlzj.Items.IndexOf(ddlzj.Items.FindByValue(d.Rows[0]["fbh"].ToString()));


            dts = d.Rows[0]["sdate"].ToString().Trim();
            if (dts.Substring(0, 4) != "1900")
            {
                lastday = DateTime.Parse(dts);
                date1.Text = lastday.ToString("yyyy-M-d");
            }

            dts = d.Rows[0]["edate"].ToString().Trim();
            if (dts.Substring(0, 4) != "1900")
            {
                lastday = DateTime.Parse(dts);
                date2.Text = lastday.ToString("yyyy-M-d");
            }


            txtmoney.Text =d.Rows[0]["money"].ToString().Trim();
            txtworktype.Text = d.Rows[0]["work"].ToString().Trim();
            txtmemo.Text = d.Rows[0]["memo"].ToString().Trim();

               

        }
  
        protected void btnSubmit_Click(object sender, EventArgs e)
        {  
                   up_member();

            

            Alert.Show("添加完成 ");
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

        void up_member()
        {
       
        

            Model.PQ.pq_ht s = new Model.PQ.pq_ht();
    s.sid =int.Parse(hidsid.Text);
          
            s.htbh = txtht.Text;

            s.pbh = ddlperson.SelectedValue;
            s.pname = ddlperson.SelectedText;
            s.pvendor = ddlcompany.SelectedText;


            if (VAN.IsDateTime(date1.Text))
                s.sdate = DateTime.Parse(date1.Text);
            else
                s.sdate = DateTime.Parse("1900-01-01");

            if (VAN.IsDateTime(date2.Text))
                s.edate = DateTime.Parse(date2.Text);
            else
                s.edate = DateTime.Parse("1900-01-01");

            if (ddlcomp.SelectedValue != "")
            {
                s.ftype = "公司";
                s.fbh = ddlcomp.SelectedValue;
                s.fname = ddlcomp.SelectedText;
            }

            if (ddlschool.SelectedValue != "")
            {
                s.ftype = "学校";
                s.fbh = ddlschool.SelectedValue;
                s.fname = ddlschool.SelectedText;
            }


            if (ddlzj.SelectedValue != "")
            {
                s.ftype = "中介";
                s.fbh = ddlzj.SelectedValue;
                s.fname = ddlzj.SelectedText;
            }

            if (ddlsales.SelectedValue != "")
            {
                s.ftype = "业务员";
                s.fbh = ddlsales.SelectedValue;
                s.fname = ddlsales.SelectedText;
                s.owen = ddlsales.SelectedText;
            }
            else
                s.owen = "";

            s.pvbh = ddlcompany.SelectedValue;
            s.httype = "company";
            s.money = 0;
            s.money = decimal.Parse(txtmoney.Text);

            s.work = "";
            s.work = txtworktype.Text;

            s.status = 0;
            s.zsales = txtzsales.Text;
            s.memo = "";
            s.memo = txtmemo.Text;
    

            BLL.PQ.pq_ht t = new BLL.PQ.pq_ht();
            t.Update(s);

            //更新业务员操作记录
            BLL.Bllbase.update_rec(tab, um, " htbh='" + s.htbh + "' ");
        }
       
    }
}